package com.base.cn.platform.os.service.manage.join;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.manage.join.SysUserRoleDao;
import com.base.cn.platform.os.entity.manage.join.SysUserRole;
import com.base.cn.platform.os.entity.manage.role.SysRole;
import com.base.cn.platform.os.service.manage.role.SysRoleBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户角色关联业务类
 *
 * @author s.li
 * @create 2018-02-05 16:32
 */
@Service
public class SysUserRoleBiz extends BaseBiz<SysUserRole,SysUserRoleDao> {

    @Autowired
    private SysRoleBiz sysRoleBiz;

    /**
     * 查询用户对象的角色列表
     * @param userId 用户ID
     * @return 用户角色列表
     */
    public List<SysRole> findSysUserRoleListByUserId(BigDecimal userId){
        if(userId!=null){
            return this.findSysUserRoleListByUserIds(userId.toString());
        }
        return null;
    }

    /**
     * 查询用户对象的角色列表
     * @param userIds 用户ID 串
     * @return 用户角色列表
     */
    public List<SysRole> findSysUserRoleListByUserIds(String userIds){
        List<SysUserRole> userRoleList = this.findSysUserRoleListByUserId(userIds);
        if(ObjectUtils.isNotEmpty(userRoleList)){
            String roleIds = userRoleList.stream().map(e->e.getRoleId().toString()).collect(Collectors.joining(","));
            logger.info("-----------------sysRoleBiz:"+sysRoleBiz);
            List<SysRole> sysRoleList = sysRoleBiz.find(" id in ("+roleIds+") and status=1",null,null);
            return sysRoleList;
        }
        return null;
    }

    /**
     * 通过用户ID串，查询用户与角色关联的中间数据记录
     * @param userIds 用户ID串
     * @return List<SysUserRole>
     */
    public List<SysUserRole> findSysUserRoleListByUserId(String userIds){
        if(StringUtils.isNotEmpty(userIds)) {
            userIds = StringUtils.subHeadTailString(userIds, ",");
            if(StringUtils.isNotEmpty(userIds)){
                return this.find(" userId in("+userIds+") ",null,null);
            }
        }
        return null;
    }

    /**
     * 创建用户与角色的关联
     * @param sysUserId 用户ID
     * @param roleIds 角色ID
     */
    public void createSysUserRole(BigDecimal sysUserId, String roleIds) {
        String[] arr = roleIds.split(",");
        List<SysUserRole> userRoleList = new ArrayList<SysUserRole>(arr.length);
        for(int i =0 ;i< arr.length; i++){
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(sysUserId);
            userRole.setRoleId(new BigDecimal(arr[i]));
            userRoleList.add(userRole);
        }
        //先删除原有的角色关联数据
        this.deleteWhereSql(" userId ="+sysUserId);
        if(!userRoleList.isEmpty()){
            //再添加新的角色关联数据
            this.batchSave(userRoleList);
        }
    }
}
